package com.validant.enigma3.common;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.ITestContext;

import com.validant.enigma3.common.Constants.Browser;
import com.validant.enigma3.common.Constants.Selector;
import com.validant.enigma3.controls.ExtWebElement;

/*
 * This class includes static common functions
 */
public class Utils {

	/*
	 * change a string to Browser enum
	 */
	public static Browser toBrowser(String browser) {
		Browser ret;

		if (browser.toLowerCase().equals("firefox")) {
			ret = Browser.Firefox;
		} else if (browser.toLowerCase().equals("chrome")) {
			ret = Browser.Chrome;
		} else if (browser.toLowerCase().equals("internet explorer")) {
			ret = Browser.InternetExplorer;
		} else if (browser.toLowerCase().equals("ie")) {
			ret = Browser.InternetExplorer;
		} else if (browser.toLowerCase().equals("safari")) {
			ret = Browser.Safari;
		} else {
			ret = Browser.Firefox;
		}

		return ret;
	}

	/*
	 * change a string to Selector enum
	 */
	public static Selector toSelector(String selector) {
		Selector ret = null;

		if (selector.toLowerCase().equals("xpath")) {
			ret = Selector.Xpath;
		} else if (selector.toLowerCase().equals("cssselector")) {
			ret = Selector.cssSelector;
		} else if (selector.toLowerCase().equals( "id")) {
			ret = Selector.Id;
		} else if (selector.toLowerCase().equals("name")) {
			ret = Selector.Name;
		} else if (selector.toLowerCase().equals("classname")) {
			ret = Selector.ClassName;
		} else if (selector.toLowerCase().equals("paritallinktext")) {
			ret = Selector.ParitalLinkText;
		} else if (selector.toLowerCase().equals("linktext")) {
			ret = Selector.LinkText;
		} else if (selector.toLowerCase().equals("tagname")) {
			ret = Selector.TagName;
		}

		return ret;
	}

	/*
	 * return corresponding locator object for an element
	 */
	public static By getLocator(Selector selector, String path) {
		By locator = null;

		if (selector == Selector.Xpath) {
			locator = By.xpath(path);
		} else if (selector == Selector.cssSelector) {
			locator = By.cssSelector(path);
		} else if (selector == Selector.Id) {
			locator = By.id(path);
		} else if (selector == Selector.Name) {
			locator = By.name(path);
		} else if (selector == Selector.ClassName) {
			locator = By.className(path);
		} else if (selector == Selector.LinkText) {
			locator = By.linkText(path);
		} else if (selector == Selector.ParitalLinkText) {
			locator = By.partialLinkText(path);
		} else if (selector == Selector.TagName) {
			locator = By.tagName(path);
		}

		return locator;
	}
	
	/*
	 * create data provider from an Excel file
	 * this Excel file name is configured in test suite files
	 */
	public static Iterator<Object[]> createDataProvider(String testdatafile, String sheetname) {
		return SpreadSheet.getTestData(testdatafile, sheetname);
	}
	
	/*
	 * get test suite parameter
	 */
	public static String getTestParameter(ITestContext context, String param) {
		return context.getCurrentXmlTest().getParameter(param);
	}
	
	/*
	 * get class name from class simple name
	 * For example: 
	 *    class simple name = Simple1@SLTests
	 *    this function returns: Simple1
	 */
	public static String getClassName(Object obj) {
		String temp = obj.getClass().getSimpleName().trim();
		int i = temp.indexOf('@') + 1;
		
		if (i > 0)
			temp = temp.substring(0, i);
		
		return temp;		
	}

	/*
	 * return screen-shot file full name
	 * Format: <project home folder>/test.reports
	 */
	public static String getScreenshotDir() {
		return Configuration.getScreenshotDir() + "/html/";
	}
	
	/*
	 * return screen-shot file name
	 * Format: <test name>.png
	 */
	public static String getScreenshotFileName(String testName) {
		return Constants.CON_SCREENSHOT_PREFIX + testName + ".png";
	}
	
	/*
	 * convert list of WebElement controls to list of ExtWebElement controls
	 */
	public static List<ExtWebElement> toExtWebElements(List<WebElement> webElements) {
		List<ExtWebElement> extWebElements = new ArrayList<ExtWebElement>();
		
		for (WebElement item : webElements) {
			extWebElements.add (new ExtWebElement(item));
        }
		
		return extWebElements;
	}
	
	// ADD MORE FUNCTIONS HERE!
}
